//
//  AppDelegate.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/8.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
//    var tabBarVC: CZTabBarController?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        NSThread .sleepForTimeInterval(1.5)
        
        window=UIWindow(frame: (UIScreen.mainScreen().bounds))
        
//        let vc = CZTabBarController()
//        let vc = HMNewFeatureViewController()
        let vc = defaultViewcontroller()

        window?.rootViewController=vc
        
        window?.makeKeyAndVisible()
        
        
        return true
    }

    
    // MARK: - ******************* ********************//是否是新特性版本
    
    private func isNewversion () -> Bool {
    // 获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        HMPrint(version)
        //获取上一次版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        HMPrint(sandboxVersion)
        //判断
        
        let newVersion = version != sandboxVersion
        
        print("\(version)asdasda \(sandboxVersion)")
        
        if newVersion {
            HMPrint("是新版本")
//            NSUserDefaults.standardUserDefaults().setValue(version, forKey: "version")
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        
        //返回判断结果
    
        return newVersion
    }
    
    // MARK: - ******************* 简单无操作界面切换 ********************
    func defaultViewcontroller ()->UIViewController {
    
        if !CZUseraccountViewModel.sharInatance.userLogin {
            HMPrint("不是主界面")
            return CZTabBarController()
        }
        let newVersion = isNewversion()
        return newVersion ? HMNewFeatureViewController() : HMWelcomeViewController()
    
    }
    
    // MARK: - ******************* 复杂又操作界面切换 ********************
    func switchViewcontroller(controller: UIViewController)->() {
    
        window?.rootViewController = controller
        
    
    
    }
    
    
    
   // MARK: - *******************  ******************** /// 设置皮肤
    private func setAppearance() {
        // 设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

